/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.midlet.*;
import javax.microedition.media.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.media.control.VolumeControl;

/**
 * @author Administrador
 */
public class Mp3Test extends MIDlet implements CommandListener {
    private Command exitCommand, playCommand, stopCommand;
    private Display display;
    private Form screen;
    private InputStream is;
    private Player player;
    private VolumeControl vc;

    public void startApp() {

        display=Display.getDisplay(this);

        exitCommand=new Command("Salir",Command.EXIT, 2);
        playCommand=new Command("Play", Command.SCREEN, 0);
        stopCommand=new Command("Stop", Command.SCREEN, 1);

        screen=new Form("PruebaAudio");
        screen.addCommand(exitCommand);
        screen.addCommand(playCommand);
        screen.setCommandListener(this);

        display.setCurrent(screen);

    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c==exitCommand){
            destroyApp(false);
            notifyDestroyed();
        }else if (c==playCommand){
            screen.removeCommand(c);
            screen.addCommand(stopCommand);
            play();
            }else try{
                    player.stop();
                    screen.removeCommand(c);
                    screen.addCommand(playCommand);
                  }catch(Exception e){};
    }

    public void play(){
        try{
            is=this.getClass().getResourceAsStream("/loco.mp3");
            screen.append(new StringItem("","\nIS Creado..."));

            player=Manager.createPlayer(is, "audio/mpeg");
            screen.append(new StringItem("","\nPlayer Creado..."));

            player.realize();
            vc=(VolumeControl) player.getControl("VolumeControl");
            vc.setLevel(100);
            player.prefetch();
            player.start();
        }catch(Exception e){
            Alert aux=new Alert("Exception", e.toString(), null, null);
            aux.setTimeout(Alert.FOREVER);
            display.setCurrent(aux);
            e.printStackTrace();
        }
    }
}